package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.chinadutyfree.retailer.core.dao.GoodsShopwindowDAO;
import com.ecoolex.chinadutyfree.retailer.entity.Goods;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsShopwindow;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsShopwindow.Column;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsShopwindowExample;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;


@Service
public class GoodsShopwindowManager {

	@Autowired
	private GoodsShopwindowDAO dao;

	public void update(Goods goods, List<String> shopwindowList, String updateUser) {
		List<GoodsShopwindow> oldList = queryByGoodsId(goods.getGoodsId());

		for (int i = 0, size = shopwindowList.size(); i < size; i++) {
			String image = shopwindowList.get(i);
			BizAssert.isTrue(Check.notNullOrTrimEmpty(image), ResultCode.VERIFY.build(20), "image为空");

			GoodsShopwindow old = oldList.stream().collect(CollectorUtil.first(f -> f.getImage().equals(image)));

			if (Check.notNull(old)) {
				old.setSorting(i + 1);
				old.setUpdateUser(updateUser);
				old.setUpdateTime(Date.from(Instant.now()));
				// 执行
				dao.updateByPrimaryKeySelective(old, Column.sorting, Column.updateUser, Column.updateTime);
				// 移除
				oldList.remove(old);
			} else {
				GoodsShopwindow record = new GoodsShopwindow();
				record.setGoodsId(goods.getGoodsId());
				record.setImage(image);
				record.setSorting(i + 1);
				record.setDeleted(GoodsShopwindow.NOT_DELETED);
				record.setCreateUser(updateUser);
				record.setCreateTime(Date.from(Instant.now()));
				record.setUpdateUser(updateUser);
				record.setUpdateTime(Date.from(Instant.now()));
				dao.insert(record);
			}
		}

		// 删除多余的
		oldList.forEach(old -> {
			old.setDeleted(GoodsShopwindow.IS_DELETED);
			old.setUpdateUser(updateUser);
			old.setUpdateTime(Date.from(Instant.now()));
			dao.updateByPrimaryKeySelective(old, Column.deleted, Column.updateUser, Column.updateTime);
		});
	}

	public List<GoodsShopwindow> queryByGoodsId(Integer goodsId) {
		GoodsShopwindowExample example = new GoodsShopwindowExample();
		example.createCriteria().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(GoodsShopwindow.NOT_DELETED);
		example.orderBy(Column.sorting.asc());
		return dao.selectByExample(example);
	}

	public void deleteByGoodsId(Integer goodsId, String updateUser) {
		GoodsShopwindowExample example = new GoodsShopwindowExample();
		example.createCriteria().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(GoodsShopwindow.NOT_DELETED);

		GoodsShopwindow record = new GoodsShopwindow();
		record.setDeleted(GoodsShopwindow.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByExampleSelective(record, example, Column.deleted, Column.updateUser, Column.updateTime);
	}
}